package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.KhachHang;
import model.NhaCC;
import model.NhanVien;

public class NhaCCController {

	public static NhaCC getNhaCC(int id) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM nhacc WHERE id=" + id + ";";
			ResultSet rs = stmt.executeQuery(sql);
			NhaCC ncc;
			if (rs.next()) {
				ncc = new NhaCC();
				ncc.setMaNCC(id);
				ncc.setTenNCC(rs.getString("ten"));
				ncc.setDiaChi(rs.getString("diachi"));
				ncc.setEmail(rs.getString("email"));
				ncc.setSdt(rs.getString("sdt"));
				return ncc;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}
	public static List<NhaCC> getDSNhaCC() {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM nhacc";
			ResultSet rs = stmt.executeQuery(sql);
			List<NhaCC> list = new ArrayList<>();
			NhaCC n;
			while (rs.next()) {
				n = new NhaCC();
				n.setMaNCC(rs.getInt("id"));
				n.setTenNCC(rs.getString("ten"));
				n.setDiaChi(rs.getString("diachi"));
				n.setEmail(rs.getString("email"));
				n.setSdt(rs.getString("sdt"));
				list.add(n);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void insertNhaCC(NhaCC n) {
		try {
			Statement sta = DBConection.MYSQL.createStatement();
			String query = "INSERT INTO nhacc (id,ten,diachi,email,sdt) VALUES (";
			query += "'" + n.getMaNCC() + "',";
			query += "'" + n.getTenNCC() + "',";
			query += "'" + n.getDiaChi() + "',";
			query += "'" + n.getEmail() + "',";
			query += "'" + n.getSdt() + "')";
			
			System.out.println(query);
			sta.execute(query);
			return;

		} catch (Exception e) {

			e.printStackTrace();
		}
	}
	public static void updateNhaCC(NhaCC n) {
		try {
			Statement st = DBConection.MYSQL.createStatement();
			String query = "UPDATE nhacc SET " + "ten=" +"'"+ n.getTenNCC()
					+ "'"+"," + "diachi= " +"'"+ n.getDiaChi() +"'"+ "," + "email="
					+ "'"+n.getEmail() +"'"+ "," + "sdt= " +"'"+ n.getSdt()
					+ "'"+" WHERE id =" + n.getMaNCC();
			System.out.println(query);
			st.execute(query);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	public static void deleteNhaCC(int id) {
		try {
			Statement st = DBConection.MYSQL.createStatement();
			String query = "DELETE FROM nhacc WHERE id = "+ id;
			st.execute(query);
			return;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	public static List<NhaCC> getNhaCC1(String name){
		Connection conn= DBConection.MYSQL;
		if(conn==null)
			return null;
		try{
			 Statement stmt = conn.createStatement();
	            String sql = "SELECT * FROM nhacc WHERE ten LIKE '%" + name + "%'";
	            sql+=" OR diachi LIKE '%"+name+"%'";
	            ResultSet rs = stmt.executeQuery(sql);
	           List<NhaCC> list = new ArrayList<>();
	            while(rs.next()) {
	             NhaCC n = new NhaCC();
	              n.setMaNCC(rs.getInt("id"));
	              n.setTenNCC(rs.getString("ten"));
	              n.setDiaChi(rs.getString("diachi"));
	              n.setEmail(rs.getString("email"));
	              n.setSdt(rs.getString("sdt"));
	              list.add(n);
	            }
	            return list;
	        } catch (Exception e) {
	            e.printStackTrace();

	            return null;
	        }
	    }


	public static void main(String[] args) {
		NhaCCController d = new NhaCCController();
		System.out.println(d.getDSNhaCC());
//		d.updateNhaCC(new NhaCC(3, "Senko", "q1, Tp.Hồ Chí Minh", "senko@gmail.com", "083652851"));
	}
	
}
